package com.example.backend.controller;

import com.example.backend.service.AdminUserService;
import com.example.backend.service.PostService;
import com.example.backend.service.SharedBookletService;
import com.example.backend.service.WordService;
import com.example.backend.util.PageUtils;
import com.example.backend.util.Result;
import com.example.backend.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员相关控制器
 * @date 2022/5/6 9:42
 */
@RestController
@RequestMapping("/v1/admin")
public class AdminController {
    @Autowired
    private WordService wordService;
    @Autowired
    private PostService postService;
    @Autowired
    private SharedBookletService sharedBookletService;
    @Autowired
    private AdminUserService adminUserService;

    /**
     * 查找单词控制器
     * @author 李月
     * @param wordSpell 单词拼写
     * @return 返回单词id和单词拼写
     */
    @GetMapping("/words/{wordSpell}")
    public Result findWordByAdmin(@PathVariable("wordSpell") String wordSpell){
        if(wordService.findWordByAdmin(wordSpell) != null){
            return ResultGenerator.genSuccessResult("查找单词成功", wordService.findWordByAdmin(wordSpell));
        } else {
            return ResultGenerator.genFailResult("未找到单词", null);
        }
    }

    /**
     * 查看单词详情控制器
     * @author 李月
     * @param wordId
     * @return 根据不同情况返回结果
     */
    @GetMapping("/words/word/{wordId}")
    public Result getWordSpecification(@PathVariable("wordId") int wordId){
        if(wordService.getWordSpecification(wordId) != null){
            return ResultGenerator.genSuccessResult("查看单词详情成功", wordService.getWordSpecification(wordId));
        } else {
            return ResultGenerator.genFailResult("查看单词详情失败", wordService.getWordSpecification(wordId));
        }
    }

    /**
     * 修改单词详情控制器
     * @author 李月
     * @param wordId 单词id
     * @param params 参数键值对，包括单词拼写、释义和音标
     * @return 根据不同情况返回结果
     */
    @PutMapping("/words/{wordId}")
    public Result modifyWordSpecification(@PathVariable("wordId") int wordId, @RequestBody Map<String, String> params){
        if(wordService.modifyWordSpecification(wordId, params)){
            return ResultGenerator.genSuccessResult("修改单词成功", null);
        } else {
            return ResultGenerator.genFailResult("修改单词失败", null);
        }
    }

    /**
     * 管理员删除单词控制器
     * @param wordId 单词id
     * @param params 参数键值对，包括管理员id和密码
     * @return 根据不同情况返回结果
     */
    @DeleteMapping("/words/{wordId}")
    public Result deleteWord(@PathVariable("wordId") int wordId, @RequestBody Map<String, Object> params){
        int adminId = (int) params.get("adminId");
        String password = (String) params.get("adminPassword");
        if(wordService.deleteWord(wordId, adminId, password) == 0){
            return ResultGenerator.genSuccessResult("删除单词成功", null);
        } else if(wordService.deleteWord(wordId, adminId, password) == 3){
            return ResultGenerator.genFailResult("管理员密码错误", null);
        } else if(wordService.deleteWord(wordId, adminId, password) == 1){
            return ResultGenerator.genFailResult("单词不存在", null);
        } else {
            return ResultGenerator.genFailResult("删除单词失败", null);
        }
    }

    /**
     * 管理员增加单词控制器
     * @author 李月
     * @param params
     * @return 根据不同情况返回结果
     */
    @PostMapping("/words")
    public Result addWord(@RequestBody Map<String, String> params){
        if(wordService.addWord(params) == 0){
            return ResultGenerator.genSuccessResult("增加单词成功", null);
        } else if(wordService.addWord(params) == 1){
            return ResultGenerator.genFailResult("单词已存在", null);
        } else {
            return ResultGenerator.genFailResult("增加单词失败", null);
        }
    }

    /**
     * 管理员无条件删帖控制器
     * @author 李月
     * @param postId 帖子id
     * @return 根据不同情况返回结果
     */
    @DeleteMapping("/post/{postId}")
    public Result deletePostByAdmin(@PathVariable("postId") int postId){
        if(postService.deletePostByAdmin(postId) == 0){
            return ResultGenerator.genSuccessResult("删帖成功", null);
        } else if(postService.deletePostByAdmin(postId) == 1){
            return ResultGenerator.genFailResult("该帖不存在", null);
        } else {
            return ResultGenerator.genFailResult("删帖失败", null);
        }
    }

    @DeleteMapping("/sharedBooklets/{bookletId}")
    public Result deleteSharedBooklet(@PathVariable("bookletId") int bookletId){
        if(sharedBookletService.deleteSharedBooklet(bookletId) == 0) {
            return ResultGenerator.genSuccessResult("删除共享单词本成功", null);
        } else {
            return ResultGenerator.genFailResult("删除共享单词本失败", null);
        }
    }
    @GetMapping("/users")
    public  Result getUsers(int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(adminUserService.getUsers(page, pageSize) != null){
            PageUtils p = adminUserService.getUsers(page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看用户列表成功", map);
        }
        else{
            return ResultGenerator.genFailResult("查看用户列表失败", null);
        }
    }
    @PostMapping("/users/user")
    public Result getUserInformation (@RequestBody Map<String, Object> params){
        if(adminUserService.getUserInformation((String)params.get("phoneNumber"),(int) params.get("userId"))!=null){
            return ResultGenerator.genSuccessResult("查询用户信息成功", adminUserService.getUserInformation((String)params.get("phoneNumber"),(int) params.get("userId")));
        }else{
            return ResultGenerator.genFailResult("查询用户信息失败", null);
        }
    }
    @PutMapping("/users/{userId}")
    public Result modifyUser (@PathVariable("userId") int userId,@RequestBody Map<String, Object> params){
        if(adminUserService.modifyUser(userId,params)==0){
            return ResultGenerator.genSuccessResult("修改用户信息成功",null);
        }
        else{
            return ResultGenerator.genFailResult("修改用户信息失败", null);
        }
    }
    @DeleteMapping("/users/{userId}")
    public Result deleteUser (@PathVariable("userId") int userId,@RequestBody Map<String, Object> params){
        if(adminUserService.deleteUser(userId,params)==0){
            return ResultGenerator.genSuccessResult("删除成功",null);
        }
        else if(adminUserService.deleteUser(userId,params)==1){
            return ResultGenerator.genFailResult("用户已删除",null);
        }
        else if(adminUserService.deleteUser(userId,params)==2){
            return ResultGenerator.genFailResult("请输入正确用户",null);
        }
        else if(adminUserService.deleteUser(userId,params)==3){
            return ResultGenerator.genFailResult("请登录管理员账户",null);
        }
        else if(adminUserService.deleteUser(userId,params)==4){
            return ResultGenerator.genFailResult("请输入正确密码",null);
        }
        else{
            return ResultGenerator.genFailResult("删除失败",null);
        }
    }
}
